<?php
require_once('_lib/SQLiteManager.php');
require_once('_lib/mostrar.php');
$dataBase = openDB("database/catalogo");
include('_lib/ajaxpaginator.class.php');

$resultado = ejecutarQuery($dataBase,"SELECT * FROM configuracion WHERE id_configuracion ='1'");
while($row = obtenerFila($resultado)){
	$recPerPage = $row['cantidad_por_pagina'];//number of records per page
	$ordenDefecto =  $row['orden_defecto'];//orden por defecto
}
if (!empty($_GET['c'])){
	if (!empty($_GET['m'])){
		echo "<div class='titulo'><img src='templates/template1/images/pelotaGrande.png'></img><h1>Modelo</h1><div class='clear' ></div></div>";
		$query = "SELECT * FROM raquetas NATURAL JOIN imagenesRaquetas NATURAL JOIN modelos NATURAL JOIN marcas WHERE (nombre_modelo = '{$_GET['m']}' AND nombre_marca = '{$_GET['c']}')";		
		}
	else
		{
		echo "<div class='titulo'><img src='templates/template1/images/raquetaGrande.png'></img><h1>Categoría</h1><div class='clear' ></div></div>";
		$query = "SELECT * FROM raquetas NATURAL JOIN imagenesRaquetas NATURAL JOIN modelos NATURAL JOIN marcas WHERE nombre_marca = '{$_GET['c']}'";
		}
	}
	else
		{
		echo "<div class='titulo'><img src='templates/template1/images/lupa.png'></img><h1>Búsqueda</h1><div class='clear' ></div></div>";
		$query = "SELECT * FROM raquetas NATURAL JOIN imagenesRaquetas NATURAL JOIN modelos NATURAL JOIN marcas ";
		}
		
		// if there is a a search query
		$searchQuery = !empty($_GET['search'])?$searchQuery = $_GET['search']:''; 
		$pageId= empty($_GET['page'])? 1:$_GET['page']; //escapar aca
		if (!empty($_GET['orden']))
		$ordenDefecto= $_GET['orden'];
		$paginator = new AjaxPaginator($pageId,$recPerPage,$query,$dataBase);
		$paginator->searchQuery = $searchQuery;
		$paginator->setOrder(" ORDER BY ".$ordenDefecto." DESC ");
		// database field to search in
		// or try array
		// passing an array makes the search text to search in the name or the id
		$paginator->fields = array('nombre_raqueta','nombre_marca','nombre_modelo','etiqueta');
		// page number, records per page, sql query, and the mysqli connection object
		//$paginator = new AjaxPaginator('1',$recPerPage,$query,$dataBase );
		$paginator->setOrder(" ORDER BY ".$ordenDefecto." DESC ");
		try{
			$rows = $paginator->paginate();
		}
		catch (Exception $e){
			echo $e->getMessage();
		}
		mostrarComboOrdenar($ordenDefecto);
		echo "<ul >";
		$i = mostrarRaqueta($rows);
		echo "</ul>";
		if($i> 0)
		{
			$links = $paginator->getLinks ();	
			echo "<div class='paginator'> " . $links ;
			echo "<p>Página " . $paginator->pageId . " de " . $paginator->totalPages . "</p>";
			echo "</div>";	
		}
		else
		{
			echo "<div class='paginator'> ";
			echo "<img src='templates/template1/images/vacio.png'></img>";
			echo "</div>";	
		}
?>
